package com.bank.maven.servlet;

import com.alibaba.fastjson.JSON;
import com.bank.maven.entity.Jilu;
import com.bank.maven.entity.User;
import com.bank.maven.entity.dao.JiluDao;
import com.bank.maven.service.impl.userserviceImpl;
import com.bank.maven.service.userservice;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/jiluServlet")
public class JiluServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String fu = req.getParameter("city");
        String shou = req.getParameter("city1");
        String time = req.getParameter("date");
        int page = req.getParameter("page") == null ? 1 : Integer.parseInt(req.getParameter("page"));
        int limit = req.getParameter("limit") == null ? 10 : Integer.parseInt(req.getParameter("limit"));
        userservice u = new userserviceImpl();
        PrintWriter out = resp.getWriter();
        try {
            List<JiluDao> list = u.getusers(fu, shou, time, page, limit);

//            out.print(JSON.toJSONString(list));

            int count = u.count(fu, shou, time); // 假设这是总记录数（实际中可能需要从数据库查询）


            Map<String, Object> result = new HashMap<>();
            result.put("code", 0);
            result.put("msg", "请求成功");
            result.put("count", count);
            result.put("data", list);
            resp.getWriter().print(JSON.toJSONString(result));

            out.flush();
            out.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException | ParseException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
